<?php

namespace App\Http\Extend;

use App\Models\User;

trait TraitLink
{
    public function scopeCreatorJoin($query, $alias = '')
    {
        $alias = $alias == '' ? $this->getTable() : $alias;
        $query->leftjoin((new User)->getTable() . " as creator", function ($join) use ($alias) {
            $join->on('creator.id', '=', "{$alias}.creator_id");
        })->addSelect("creator.name as creator_name");
        return $query;
    }

    public function scopeEditorJoin($query, $alias = '')
    {
        $alias = $alias == '' ? $this->getTable() : $alias;
        $query->leftjoin((new User)->getTable() . " as editor", function ($join) use ($alias) {
            $join->on('editor.id', '=', "{$alias}.editor_id");
        })->addSelect("editor.name as editor_name");
        return $query;
    }

    public function scopeAuditorJoin($query, $alias = '')
    {
        $alias = $alias == '' ? $this->getTable() : $alias;
        $query->leftjoin((new User)->getTable() . " as auditor", function ($join) use ($alias) {
            $join->on('auditor.id', '=', "{$alias}.auditor_id");
        })->addSelect("auditor.name as auditor_name");
        return $query;
    }
}
